package com.easy.component.loadingmore;

import android.content.Context;
import android.os.SystemClock;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.MotionEvent;


/**
 * 描述: {描述}
 * 作者: jack-song
 * 创建日期: 2017/11/15 10:31
 * 修改日期: 2017/11/15
 * 版本: {版本}
 */
public class MyRecyclerViewMore extends RecyclerView {
    private static final int ENDINT_LOADING = 1;
    private static final int ENDINT_LOAD_DONE = 2;
    private static final int ENDINT_LOAD_NO_MORE = 3;
    private int mEndState;
    private boolean mCanLoadMore = false;
    private MoreView loadMoreView;
    private OnLoadMoreListener mLoadMoreListener;
    private HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter;
    private RecyclerView.AdapterDataObserver mDataObserver = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            MyRecyclerViewMore.this.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), 3, 0.0F, 0.0F, 0));
        }
    };

    public MyRecyclerViewMore(Context context) {
        super(context);
        this.init(context, (AttributeSet)null);
    }

    public MyRecyclerViewMore(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init(context, attrs);
    }

    public MyRecyclerViewMore(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.mHeaderAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter();
        super.setAdapter(this.mHeaderAndFooterRecyclerViewAdapter);
        this.loadMoreView = new BaseMoreView(context);
        this.mEndState = 2;
        this.setHasLoadMore(false);
        this.addOnScrollListener(new MyRecyclerViewMore.RecyclerViewOnScrollListener(null));
    }

    public void setCanLoadMore(boolean pCanLoadMore) {
        this.mCanLoadMore = pCanLoadMore;
        if(this.mCanLoadMore && this.mHeaderAndFooterRecyclerViewAdapter.getFootersCount().intValue() == 0) {
            this.addFooterView();
        }

        if(!this.mCanLoadMore && this.mHeaderAndFooterRecyclerViewAdapter.getFootersCount().intValue() > 0) {
            this.mHeaderAndFooterRecyclerViewAdapter.removeFooter(this.loadMoreView.getFooterView());
        }

    }

    public void setEndintLoading() {
        this.mEndState = 1;
    }

    public void onLoadMoreComplete() {
        this.mEndState = 2;
    }

    private void addFooterView() {
        if(this.mHeaderAndFooterRecyclerViewAdapter.getFootersCount().intValue() == 0) {
            this.mHeaderAndFooterRecyclerViewAdapter.addFooterView(this.loadMoreView.getFooterView());
        }

    }

    @Override
    public void setAdapter(Adapter adapter) {
        try {
            adapter.unregisterAdapterDataObserver(this.mDataObserver);
        } catch (Exception var3) {
            ;
        }

        adapter.registerAdapterDataObserver(this.mDataObserver);
        this.mHeaderAndFooterRecyclerViewAdapter.setAdapter(adapter);
    }

    public void setOnLoadListener(OnLoadMoreListener pLoadMoreListener) {
        if(pLoadMoreListener != null) {
            this.mLoadMoreListener = pLoadMoreListener;
        }

    }

    private void loadMore() {
        this.mLoadMoreListener.loadMore();
    }

    public void setHasLoadMore(boolean hasLoadMore) {
        if(hasLoadMore) {
            this.showNormalUI();
        } else {
            this.showNoMoreUI();
        }

    }

    public void showNormalUI() {
        this.loadMoreView.showNormal();
    }

    public void showLoadingUI() {
        this.loadMoreView.showLoading();
    }

    public void showNoMoreUI() {
        this.mEndState = 3;
        this.loadMoreView.showNoMore();
    }

    public void showFailUI() {
        this.loadMoreView.showFail();
    }

    private class RecyclerViewOnScrollListener extends OnScrollListener {
        private int[] lastPositions;
        private int lastVisibleItemPosition;
        private int currentScrollState;

        private RecyclerViewOnScrollListener(Object o) {
            this.currentScrollState = 0;
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LayoutManager layoutManager = recyclerView.getLayoutManager();
            if(layoutManager instanceof LinearLayoutManager) {
                this.lastVisibleItemPosition = ((LinearLayoutManager)layoutManager).findLastVisibleItemPosition();
            } else if(layoutManager instanceof GridLayoutManager) {
                this.lastVisibleItemPosition = ((GridLayoutManager)layoutManager).findLastVisibleItemPosition();
            } else {
                if(!(layoutManager instanceof StaggeredGridLayoutManager)) {
                    throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
                }

                StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager)layoutManager;
                if(this.lastPositions == null) {
                    this.lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                }

                staggeredGridLayoutManager.findLastVisibleItemPositions(this.lastPositions);
                this.lastVisibleItemPosition = this.findMax(this.lastPositions);
            }

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            this.currentScrollState = newState;
            LayoutManager layoutManager = recyclerView.getLayoutManager();
            int visibleItemCount = layoutManager.getChildCount();
            int totalItemCount = layoutManager.getItemCount();
            if(visibleItemCount > 0 && this.currentScrollState == 0 && this.lastVisibleItemPosition >= totalItemCount - 1 && MyRecyclerViewMore.this.mEndState == 2) {
                MyRecyclerViewMore.this.mEndState = 1;
                MyRecyclerViewMore.this.showLoadingUI();
                MyRecyclerViewMore.this.loadMore();
            }

        }

        private int findMax(int[] lastPositions) {
            int max = lastPositions[0];
            int[] var3 = lastPositions;
            int var4 = lastPositions.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                int value = var3[var5];
                if(value > max) {
                    max = value;
                }
            }

            return max;
        }
    }
}
